package com.zj.core.ui

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.design.widget.FloatingActionButton
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.ViewManager
import android.widget.LinearLayout
import com.mcxtzhang.swipemenulib.SwipeMenuLayout
import com.zj.core.R
import com.zj.core.view.MeuseEditText
import org.jetbrains.anko.*
import org.jetbrains.anko.custom.ankoView

class RecordListItemUi: AnkoComponent<ViewGroup> {
    @RequiresApi(Build.VERSION_CODES.M)
    @SuppressLint("RestrictedApi")
    override fun createView(ui: AnkoContext<ViewGroup>): View= with(ui){
        swipeMenuLayout{
            isClickable=true
            id= R.id.record_item_swip_layout
            val params= LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
            params.setMargins(0,5,0,5)
            layoutParams=params

            relativeLayout{
                lparams(width= matchParent,height = wrapContent)

                floatingActionBu{
                    id=R.id.record_item_float_bu
                    visibility=View.GONE
                    foregroundGravity= Gravity.CENTER
                    foreground =resources.getDrawable(R.drawable.ic_plus_add_24dp)
                    backgroundTintList = ColorStateList.valueOf(Color.WHITE)
                }.lparams(width= wrapContent,height = wrapContent){
                    margin=dip(5)
                    gravity= Gravity.CENTER
                    centerInParent()
                }

                relativeLayout{
                    background=context.resources.getDrawable(R.drawable.bordle_line_record_item)
                    lparams(width= matchParent,height = wrapContent)
                    setPadding(5,0,0,0)
                    id=R.id.record_item_parent_view

                    editText{
                        setText("标题")
                        background=null
                        textSize= 14F
                        singleLine=true
                        id=R.id.record_item_name_tv
                    }.lparams(width= matchParent,height = wrapContent){
                        alignParentLeft()
                        leftOf(R.id.record_item_down_img)
                    }

                    meuseEditText{
                        setText("数据")
                        background=null
                        textSize= 14F
                        singleLine=true
                        gravity=Gravity.CENTER
                        id=R.id.record_item_tv
                    }.lparams(width=matchParent,height =wrapContent){
                        alignParentLeft()
                        below(R.id.record_item_name_tv)
                        leftOf(R.id.record_item_down_img)
                    }

                    imageView{
                        id=R.id.record_item_down_img
                        visibility=View.INVISIBLE
                        image=context.resources.getDrawable(R.mipmap.record_down_img)
                    }.lparams(width= dip(40),height =wrapContent){
                        alignParentRight()
                        sameTop(R.id.record_item_name_tv)
                        sameBottom(R.id.record_item_tv)
                    }
                }.lparams{
                    leftMargin=dip(10)
                    rightMargin=dip(10)
                }
            }

            button{
                id=R.id.record_item_delete_bu
                visibility=View.VISIBLE
                layoutParams=LinearLayout.LayoutParams(100,LinearLayout.LayoutParams.MATCH_PARENT)
                backgroundColor=R.color.colorsRed
                textColor= Color.WHITE
                text="删除"
            }
        }
    }

    private inline fun ViewManager.meuseEditText(theme: Int = 0, init: MeuseEditText.() -> Unit) : MeuseEditText {
        return ankoView({ MeuseEditText(it) }, theme, init)
    }

    private inline fun ViewManager.floatingActionBu(theme: Int = 0, init: FloatingActionButton.() -> Unit) : FloatingActionButton {
        return ankoView({ FloatingActionButton(it) }, theme, init)
    }

    private inline fun ViewManager.swipeMenuLayout(theme: Int = 0, init: SwipeMenuLayout.() -> Unit) : SwipeMenuLayout {
        return ankoView({ SwipeMenuLayout(it) }, theme, init)
    }
}